package com.cvit.utils;

import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cvit.model.Examinationtable;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class PDFUpload {
	static float lineHeight = (float)20.0;
	public static void produce(HttpServletRequest request,HttpServletResponse response ,Examinationtable data1,Examinationtable data2) throws Exception{
			
		try {
		OutputStream os = response.getOutputStream();
		response.setHeader("Content-Disposition", "attachment; filename=\"" + data1.getEamineeId() + ".pdf");  
        response.setContentType("application/octet-stream;charset=UTF-8");  
	    Document document = new Document(PageSize.A4);   
		PdfWriter.getInstance(document, os);
		
		//设置字体  
        BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/simhei.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);     
        com.itextpdf.text.Font FontChinese20 = new com.itextpdf.text.Font(bfChinese, 20, com.itextpdf.text.Font.BOLD);
        com.itextpdf.text.Font FontChinese201 = new com.itextpdf.text.Font(bfChinese, 20, com.itextpdf.text.Font.NORMAL);
        com.itextpdf.text.Font FontChinese18 = new com.itextpdf.text.Font(bfChinese, 18, com.itextpdf.text.Font.BOLD);
        com.itextpdf.text.Font FontChinese14 = new com.itextpdf.text.Font(bfChinese, 14, com.itextpdf.text.Font.NORMAL);
        com.itextpdf.text.Font FontChinese141 = new com.itextpdf.text.Font(bfChinese, 14, com.itextpdf.text.Font.BOLD);
        com.itextpdf.text.Font FontChinese12 = new com.itextpdf.text.Font(bfChinese, 12, com.itextpdf.text.Font.NORMAL);
        com.itextpdf.text.Font FontChinese121 = new com.itextpdf.text.Font(bfChinese, 12, com.itextpdf.text.Font.BOLD);
        com.itextpdf.text.Font FontChinese11 = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.ITALIC);  
        com.itextpdf.text.Font FontChinese10Normal = new com.itextpdf.text.Font(bfChinese, 10, com.itextpdf.text.Font.NORMAL);
        com.itextpdf.text.Font FontChinese10 = new com.itextpdf.text.Font(bfChinese, 9, com.itextpdf.text.Font.BOLD);
        com.itextpdf.text.Font FontChinese9Normal = new com.itextpdf.text.Font(bfChinese, 9, com.itextpdf.text.Font.NORMAL);
        document.open();
        
        Paragraph pg_bt = new Paragraph("2018年吉林省高职高专学校单独招生考试",FontChinese201);
        Paragraph pg_bt1 = new Paragraph("准  考  证",FontChinese18);
        pg_bt.setAlignment(Element.ALIGN_CENTER);  
        pg_bt1.setAlignment(Element.ALIGN_CENTER);
        document.add(pg_bt);
        document.add(pg_bt1);
        Paragraph br = new Paragraph("   ",FontChinese12);
        document.add(Chunk.NEWLINE);
        //创建一个两列的表格
        BaseColor borderColor = new BaseColor(255,255,255);
        PdfPTable headerTable = new PdfPTable(2);
        int widthHeader[] = {160,40};
        headerTable.setWidths(widthHeader);
        PdfPTable leftTable = new PdfPTable(2);
        int width[] = {90,70};
        leftTable.setWidths(width);
        PdfPCell cell1 = new PdfPCell(new Paragraph("姓    名： "+data1.getName(),FontChinese12));
        cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell1.setFixedHeight(lineHeight);
        cell1.setBorderColor(borderColor);
        leftTable.addCell(cell1);
        PdfPCell cell2 = new PdfPCell(new Paragraph("性    别： "+data1.getGender(),FontChinese12));
        cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell2.setFixedHeight(lineHeight);
        cell2.setBorderColor(borderColor);
        leftTable.addCell(cell2);
        PdfPCell cell3 = new PdfPCell(new Paragraph("准考证号： "+data1.getStudentNumber(),FontChinese12));
        cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell3.setFixedHeight(lineHeight);
        cell3.setColspan(2);
        cell3.setBorderColor(borderColor);
        leftTable.addCell(cell3);
        PdfPCell cell4 = new PdfPCell(new Paragraph("考 生 号： "+data1.getEamineeId(),FontChinese12));
        cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell4.setFixedHeight(lineHeight);
        cell4.setColspan(2);
        cell4.setBorderColor(borderColor);
        leftTable.addCell(cell4);
        PdfPCell cell5 = new PdfPCell(new Paragraph("身份证号： "+data1.getCardId(),FontChinese12));
        cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell5.setFixedHeight(lineHeight);
        cell5.setColspan(2);
        cell5.setBorderColor(borderColor);
        leftTable.addCell(cell5);
        PdfPCell cell6 = new PdfPCell(new Paragraph("审核序号： "+data1.getExamineNumber(),FontChinese12));
        cell6.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell6.setFixedHeight(lineHeight);
        cell6.setColspan(2);//合并单元格
        cell6.setBorderColor(borderColor);
        leftTable.addCell(cell6);
        PdfPCell cell = new PdfPCell(leftTable);
        cell.setPadding(0);
        headerTable.addCell(cell);
        
        //5、创建图片对象,加入headerTable中,此处写入图片路径
		String dir = request.getSession().getServletContext().getRealPath("/");//总路径
		System.out.println();
		String path = dir + data1.getImage();
        Image image = Image.getInstance(path.replace("\\","\\\\"));//转译
        headerTable.addCell(image);
        //将主要的表格headerTable加入document文档对象中
        document.add(headerTable);
        document.add(br);
        PdfPTable table = new PdfPTable(3);
        BaseColor borderColor1 = new BaseColor(000,000,000);
        int width1[] = {45,45,130};
        table.setWidths(width1);
        PdfPCell cell11 = new PdfPCell(new Paragraph("考试时间",FontChinese14));
        PdfPCell cell22 = new PdfPCell(new Paragraph("考试科目",FontChinese14));
        PdfPCell cell33 = new PdfPCell(new Paragraph("考点、考场",FontChinese14));
        //表格高度
        cell11.setFixedHeight(20);
        cell22.setFixedHeight(20);
        cell33.setFixedHeight(20);
        //水平居中
        cell11.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell22.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell33.setHorizontalAlignment(Element.ALIGN_CENTER);
        //垂直居中
        cell11.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell22.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell33.setVerticalAlignment(Element.ALIGN_MIDDLE);
        //边框颜色 
        cell11.setBorderColor(borderColor1);
        cell22.setBorderColor(borderColor1);
        cell33.setBorderColor(borderColor1);
        
        table.addCell(cell11);
        table.addCell(cell22);
        table.addCell(cell33);
        document.add(table);
        
        PdfPTable table1 = new PdfPTable(3);
        table1.setWidths(width1);
        PdfPCell cell112 = new PdfPCell(new Paragraph(" "+data1.getExaminationTime()+"\n"+" "+data1.getTestTime(),FontChinese10Normal));
        //PdfPCell cell222 = new PdfPCell(new Paragraph(" "+data1.getOverall(),FontChinese10Normal));
        PdfPCell cell222 = new PdfPCell(new Paragraph("文化基础"+"\n"+"水平测试",FontChinese10Normal));
        PdfPCell cell332 = new PdfPCell(new Paragraph(" "+data1.getExaminationPlace()+"\n"+" 第"+data1.getExaminationRoom()+"考场",FontChinese10Normal));
        //表格高度
        cell112.setFixedHeight(40);
        cell222.setFixedHeight(40);
        cell332.setFixedHeight(40);
        //水平居中
        //cell112.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell222.setHorizontalAlignment(Element.ALIGN_CENTER);
        //cell332.setHorizontalAlignment(Element.ALIGN_CENTER);
        //垂直居中
        cell112.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell222.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell332.setVerticalAlignment(Element.ALIGN_MIDDLE);
        //边框颜色 
        cell112.setBorderColor(borderColor1);
        cell222.setBorderColor(borderColor1);
        cell332.setBorderColor(borderColor1);
        
        table1.addCell(cell112);
        table1.addCell(cell222);
        table1.addCell(cell332);
        document.add(table1);
        
        PdfPTable table2 = new PdfPTable(3);
        table2.setWidths(width1);
        PdfPCell cell113 = new PdfPCell(new Paragraph(" "+data2.getExaminationTime()+"\n"+" "+data2.getTestTime(),FontChinese10Normal));
        //PdfPCell cell223 = new PdfPCell(new Paragraph(" "+data2.getOverall(),FontChinese10Normal));
        PdfPCell cell223 = new PdfPCell(new Paragraph("综合素质"+"\n"+"测"+"    "+"试",FontChinese10Normal));
        PdfPCell cell333 = new PdfPCell(new Paragraph(" "+data2.getExaminationPlace()+"\n"+" "+data2.getExaminationRoom(),FontChinese10Normal));
        
        //表格高度
        cell113.setFixedHeight(40);
        cell223.setFixedHeight(40);
        cell333.setFixedHeight(40);
        
        //水平居中
        //cell113.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell223.setHorizontalAlignment(Element.ALIGN_CENTER);
        //cell333.setHorizontalAlignment(Element.ALIGN_CENTER);
        
        //垂直居中
        cell113.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell223.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell333.setVerticalAlignment(Element.ALIGN_MIDDLE);
        
        //边框颜色 
        cell113.setBorderColor(borderColor1);
        cell223.setBorderColor(borderColor1);
        cell333.setBorderColor(borderColor1);
        
        table2.addCell(cell113);
        table2.addCell(cell223);
        table2.addCell(cell333);
        document.add(table2);
        
        
        document.add(Chunk.NEWLINE); 
        PdfPTable table3 = new PdfPTable(1);
        PdfPCell cells = new PdfPCell(new Paragraph("考 生 须 知",FontChinese141));
        cells.setHorizontalAlignment(Element.ALIGN_CENTER);
        cells.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cells.setBorderColor(borderColor);
        table3.addCell(cells);
        
        
        PdfPCell cells1 = new PdfPCell(new Paragraph("笔试考生须知： ",FontChinese10));
        cells1.setBorderColor(borderColor);
        table3.addCell(cells1);
        PdfPCell cells2 = new PdfPCell(new Paragraph("      1.考场统一配备考试文具（笔、涂卡器、橡皮等），考生无需自带。",FontChinese9Normal));
        cells2.setBorderColor(borderColor);
        table3.addCell(cells2);
        PdfPCell cells3 = new PdfPCell(new Paragraph("      2.考试中途不允许去厕所，不准提前交卷离开考场。",FontChinese9Normal));
        cells3.setBorderColor(borderColor);
        table3.addCell(cells3);
        
        PdfPCell cells4 = new PdfPCell(new Paragraph("机考考生须知： ",FontChinese10));
        cells4.setBorderColor(borderColor);
        table3.addCell(cells4);
        PdfPCell cells5 = new PdfPCell(new Paragraph("      1.考试采取上机操作方式，不得将任何书籍、资料、计算器、移动存储设备、纸、笔、电脑等带入考场。",FontChinese9Normal));
        cells5.setBorderColor(borderColor);
        table3.addCell(cells5);
        PdfPCell cells6 = new PdfPCell(new Paragraph("      2.严格按考场监考教师要求操作，不得擅自进行冷、热启动、复位及其他与考试无关的任何操作，否则后果自负。",FontChinese9Normal));
        cells6.setBorderColor(borderColor);
        table3.addCell(cells6);
        PdfPCell cells7 = new PdfPCell(new Paragraph("      3.考试过程中，非个人原因导致的机器故障，向监考人员举手示意，服从安排。",FontChinese9Normal));
        cells7.setBorderColor(borderColor);
        table3.addCell(cells7);
        PdfPCell cells8 = new PdfPCell(new Paragraph("      4.每场考试时间为50分钟，在各场次开考前30分钟准时进入考场，考试结束前不得离开考场。",FontChinese9Normal));
        cells8.setBorderColor(borderColor);
        table3.addCell(cells8);
        
        PdfPCell cells9 = new PdfPCell(new Paragraph("面试考生须知： ",FontChinese10));
        cells9.setBorderColor(borderColor);
        table3.addCell(cells9);
        PdfPCell cells10 = new PdfPCell(new Paragraph("      1.考生在考前30分钟进入考点，服从管理，按照工作人员引导的路线行走。",FontChinese9Normal));
        cells10.setBorderColor(borderColor);
        table3.addCell(cells10);
        PdfPCell cells11 = new PdfPCell(new Paragraph("      2.携带本人身份证和准考证入场，保持肃静，严禁交流，严格遵守面试纪律。",FontChinese9Normal));
        cells11.setBorderColor(borderColor);
        table3.addCell(cells11);
        
        
        PdfPCell cells12 = new PdfPCell(new Paragraph("其他说明： ",FontChinese10));
        cells12.setBorderColor(borderColor);
        table3.addCell(cells12);
        PdfPCell cells13 = new PdfPCell(new Paragraph("      1.考生只需持准考证和第二代居民身份证，按规定时间和地点参加考试。开考15分钟后考生禁止进入考场。",FontChinese9Normal));
        cells13.setBorderColor(borderColor);
        table3.addCell(cells13);
        PdfPCell cells14 = new PdfPCell(new Paragraph("      2.考场统一准备石英钟和考试所用文具。考生严禁将包及移动电话带入考点。不得携带与考试无关物品。",FontChinese10));
        cells14.setBorderColor(borderColor);
        table3.addCell(cells14);
        PdfPCell cells15 = new PdfPCell(new Paragraph("      3.自觉接受和服从三次入场检查。开考后迟到的考生，必须接受入场检查，由此延误的考试时间由考生自负。",FontChinese9Normal));
        cells15.setBorderColor(borderColor);
        table3.addCell(cells15);
        PdfPCell cells16 = new PdfPCell(new Paragraph("      4.不准提前交卷，当科考试结束前未经允许不得离开考场。",FontChinese9Normal));
        cells16.setBorderColor(borderColor);
        table3.addCell(cells16);
        PdfPCell cells17 = new PdfPCell(new Paragraph("      5.如不遵守考场纪律、不服从考试工作人员管理，有违纪、作弊等行为，按照《国家教育考试违规处理办法》（教育部令第33号）及《中华人民共和国刑法修正案（九）》等有关文件进行处理，并将违规事实记入考生电子档案。",FontChinese9Normal));
        cells17.setBorderColor(borderColor);
        table3.addCell(cells17);
        
        document.add(table3);
        
        //关闭文档对象，注：当文档对象真正关闭后，数据才会写入文件中。
        document.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
}
